<?php
namespace app\admin\controller;
use app\AdminBaseController;
use think\facade\Request;
use think\facade\Filesystem;
use think\facade\Db;
use app\admin\model\PhotoAlbumModel;
use app\admin\model\MediaModel;


class Elegant extends AdminBaseController {

    public function getSinglePage() {
        $id = $_GET['id'];
        $data = Db::table('yhq_single_page')->where('id',$id)->find();
        return array('success'=>true,'content'=>$data['content']);
    }

    public function updateSinglePage() {
        $id = $_POST['id'];
        $params = array(
            'content'=>$_POST['content']
        );
        $data = Db::table('yhq_single_page')->where('id',$id)->update($params);
        if($data) {
            return array('success'=>true,'message'=>'编辑成功！');
        } else {
            return array('success'=>false,'message'=>'编辑失败！');
        }
    }

    public function getPhotoAlbums() {
        $list = PhotoAlbumModel::select();
        return array('success'=>true,'list'=>$list);
    }

    public function createAlbum() {
        $file = Request::file('file');
        $coverPath;
        if($file) {
            $result = $this->uploadImg($file,'album');
            if($result&&!$result['success']) {
                echo json_encode($result);
                die();
            }
            $coverPath = $result['path'];
        }

        $data = array(
            'title'=>$_POST['name'],
            'addtime'=>time(),
            'updatetime'=>time(),
        );
        if(isset($_POST['desc'])&&!empty($_POST['desc'])) {
            $data['desc'] = $_POST['desc'];
        }
        if(isset($coverPath)) {
            $data['cover'] = $coverPath;
        }
        $res = PhotoAlbumModel::insert($data);
        if($res) {
            echo json_encode(array('success'=>true,'message'=>'创建成功！'));
        } else {
            echo json_encode(array('success'=>false,'message'=>'创建失败！'));
        }
        
    }

    public function updateAlbum() {
        $file = Request::file('file');
        $coverPath;
        if($file) {
            $result = $this->uploadImg($file,'album');
            if($result&&!$result['success']) {
                echo json_encode($result);
                die();
            }
            $coverPath = $result['path'];
        }               

        $data = array(
            'title'=>$_POST['name'],
            'updatetime'=>time(),
        );
        if(isset($_POST['desc'])&&!empty($_POST['desc'])) {
            $data['desc'] = $_POST['desc'];
        }
        if(isset($coverPath)) {
            $data['cover'] = $coverPath;
        }
        $item = PhotoAlbumModel::where('id',$_POST['id'])->find();
        $res = PhotoAlbumModel::where('id',$_POST['id'])->update($data);
        if($coverPath&&$item['cover']) {
            $proPath = public_path().'storage/'.$item['cover'];
            if(file_exists($proPath)) {
                unlink($proPath);
            }
        }
        if($res) {
            echo json_encode(array('success'=>true,'message'=>'编辑成功！'));
        } else {
            echo json_encode(array('success'=>false,'message'=>'编辑失败！'));
        }
        
    }


    protected function uploadImg($file,$folder) {
        $result;
        try{
            validate(['imgFile'=>[
                'fileSize' => 410241024,
                'fileExt' => 'jpg,jpeg,png,bmp,gif',
                'fileMime' => 'image/jpeg,image/png,image/gif',
            ]])->check(['file' => $file]);
            $saveName = Filesystem::disk('public')->putFile( $folder, $file, 'data');
            $result = array('success'=>true,'path'=>$saveName);
        } catch (\Exception $e) {
            $result = array('success'=>false,'message'=>$e->getMessage());
        }
        return $result;
    }


    public function getAlbumPics() {
        $id = $_GET['id'];
        $item = PhotoAlbumModel::field('title,desc,cover')->where('id',$id)->find();
        if($item) {
            $count = MediaModel::where('fid',$id)->count('id');
            $list = $list = MediaModel::where('fid',$id)->order('updatetime desc')->select();
            foreach($list as $value) {
                $value['addtime'] = date('Y-m-d H:m:s',$value['addtime']);
                $value['updatetime'] = date('Y-m-d H:m:s',$value['updatetime']);
            }
            return array(
                'count'=>$count,
                'list'=>$list,
                'detail'=>$item,
                'success'=>true
            );
        } else {
            return array(
                'success'=>false,
                'message'=>'不存在当前相册'
            );
        }
    }

    public function addPicToAlbum() {
        $file = Request::file('file');
        $result = $this->uploadImg($file,'photos');
        $arr = explode('.',$result['path']);
        $thumb = $arr[0].'_thumb.'.$arr[1];
        $imgSize = getimagesize(public_path().'storage/'.$result['path']);
        $percent = $imgSize[0]/300;
        $heightSize = $imgSize[1]/$percent;
        $image = \think\Image::open(public_path().'storage/'.$result['path']);
        $image->thumb(300,$heightSize);
        $image->save(public_path().'storage/'.$thumb);
        
        
        $params = array(
            'fid'=>$_POST['fid'],
            'title'=>$_POST['name'],
            'desc'=>$_POST['desc'],
            'path'=>$result['path'],
            'thumb'=>$thumb,
            'type'=>1,
            'addtime'=>time(),
            'updatetime'=>time()
        );
        $data = MediaModel::insert($params);
        if($data) {
            echo json_encode(array('success'=>true,'message'=>'添加成功！','info'=>$file));
        } else {
            echo json_encode(array('success'=>false,'message'=>'添加失败！'));
        }
    }

    public function editImgInfo() {
        $params = array(
            'title'=>$_POST['name'],
            'desc'=>$_POST['desc'],
            'updatetime'=>time()
        );
        $data = MediaModel::where('id',$_POST['id'])->update($params);
        if($data) {
            return array('success'=>true,'message'=>'编辑成功！');
        } else {
            return array('success'=>false,'message'=>'编辑失败！');
        }
    }


    public function deleteImg() {
        $ids = explode(',',$_GET['ids']);
        $arr = MediaModel::field('thumb,path')->where('id','in',$ids)->select();
        foreach($arr as $key=>$value) {
            if(file_exists(public_path().'storage/'.$value['path'])) {
                unlink(public_path().'storage/'.$value['path']);
            }
            if(file_exists(public_path().'storage/'.$value['thumb'])) {
                unlink(public_path().'storage/'.$value['thumb']);
            }
        }
        $reslut = MediaModel::where('id','in',$ids)->delete();
        if($reslut) {
            return array('success'=>true,'message'=>'删除成功！','num'=>$reslut);
        } else {
            return array('success'=>false,'message'=>'删除失败！','num'=>$reslut);
        }
    }

    public function delAlbum() {
        $id = $_GET['id'];
        $album = PhotoAlbumModel::find($id);
        if($album['cover']) {
            if(file_exists(public_path().'storage/'.$album['cover'])) {
                unlink(public_path().'storage/'.$album['cover']);
            }
        }
        $result = PhotoAlbumModel::where('id','=',$id)->delete();
        $pics = MediaModel::where('fid','=',$id)->select();        
        $reslut2;
        if(count($pics)>0) {
            foreach($pics as $key=>$value) {
                if(file_exists(public_path().'storage/'.$value['path'])) {
                    unlink(public_path().'storage/'.$value['path']);
                }
                if(file_exists(public_path().'storage/'.$value['thumb'])) {
                    unlink(public_path().'storage/'.$value['thumb']);
                }
            }
            $reslut2 = MediaModel::where('fid','=',$id)->delete();
        }
        $res;
        if($result) {
            if(count($pics)>0) {
                if($reslut2) {
                    $res=array('success'=>true,'message'=>'删除成功！');
                } else {
                    $res=array('success'=>false,'message'=>'删除失败！');
                }
            } else {
                $res=array('success'=>true,'message'=>'删除成功！');
            }
        } else {
            $res=array('success'=>false,'message'=>'删除失败！');
        }
        return $res;
    }
}